<template>
  <div class="app-container" style="">
    <div class="search-header">
      <el-form ref="form" :model="form" label-width="80px" class="form-block">
        <el-form-item label="线路名称">
          <el-input v-model="form.name" style="display:block" />
        </el-form-item>
        <el-form-item label="线路类型">
          <el-select v-model="form.region" placeholder="请选择线路类型">
            <el-option label="普通线路" value="shanghai" />
            <el-option label="BRT线路" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="线路公司">
          <el-select v-model="form.region" placeholder="请选择线路公司">
            <el-option label="公司1" value="shanghai" />
            <el-option label="公司2" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content">
      <el-row style="padding:20px">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="add_line">新增线路</el-button>

      </el-row>
      <el-table
        :data="lineData"
        style="width: 100%"
      >
        <el-table-column
          label="线路名称"
          width="220"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="流水/固定班"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.ls_type }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="线路类型"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.line_type }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="票价"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.price }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="里程"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.distance }}公里</span>
          </template>
        </el-table-column>
        <el-table-column
          label="预售周期"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.yu_sale }}天</span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.status }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
</template>

<script>
import { aysnc_List, delete_line } from '@/api/ctm_bus'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      list: null,
      listLoading: true,
      lineData: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add_line() {
      this.$router.push({ path: '/ctmLine/add_line' })
    },
    handleEdit(index, row) {
      this.$router.push({ name: 'addCtmLine', params: {
        id: row.id
      }})
    },
    onSubmit() {
      console.log('submit!')
    },
    fetchData() {
      this.listLoading = true
      aysnc_List().then(res => {
        console.log(res)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.lineData = res.data.data
        this.listLoading = false
      })
    },
    handleDelete(index, row) {
      delete_line({ id: row.id }).then(res => {
        console.log(res.code)
        if (res.code === 200) {
          this.fetchData()
          this.$message({ showClose: true, message: res.msg, type: 'success' })
        } else {
          this.$message({ showClose: true, message: res.msg, type: 'error' })
        }
      }).catch(error => {
        this.$message({ showClose: true, message: error, type: 'error' })
      })
    }
  }
}
</script>
<style scoped>
.app-container{
  background:#f0f2f5;
}
.search-header{
  background: #fff;
  padding: 10px 20px;
  margin: 10px 10px;
  display: flex;
}
.form-block {
  display:flex;
  padding: 10px 20px;
  margin: 10px 0;
}
.content{
  margin: 10px 10px;
   background: #fff;

}
</style>
